//
//  CYTrendLotteryView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/24.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTrendLotteryView.h"
#import "CYSelectBtn.h"
@interface CYTrendLotteryView (){
    lotteryBlock _myblock;
}
@property (nonatomic, strong) NSArray *lotteries;
@property (nonatomic, copy) NSString *lotterName;

@end
@implementation CYTrendLotteryView
- (instancetype)initWithColor:(UIColor *)color currentTitle:(NSString *)lotterName titles:(NSArray *)lotteries block:(lotteryBlock)block{
    self = [super initWithColor:color];
    if (self) {
        _myblock = block;
        _lotteries = lotteries;
        _lotterName = lotterName;
        self.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH, (ceil([_lotteries count] / 3.0)) * AL(33) + (ceil([_lotteries count] / 3.0) + 1) * AL(10));
        [self setupSubview];
        
    }
    return self;
}

- (void)setCurrentIndex:(NSUInteger)currentIndex{
    if (_currentIndex != currentIndex)
        _currentIndex = currentIndex;
    if (_currentIndex > _lotteries.count - 1)//如果坐标大于数组的count
        return;
    [self changeCurrentIndex:currentIndex];
}

- (void)setupSubview{
    
    CGFloat offset_x = AL(10);
    CGFloat width = (SCREEN_WIDTH  - 4 * offset_x) / 3.0;
    CGFloat height = AL(33);
    for (int i = 0; i < _lotteries.count; i++) {
        CYSelectBtn *button = [[CYSelectBtn alloc]initWithTitle:_lotteries[i][@"name"] size:AL(PT(26)) selectImage:@"gouxuan" block:nil];
        [button addTarget:self action:@selector(actionForBtn:) forControlEvents:UIControlEventTouchUpInside];
        button.selected = ([_lotterName isEqualToString:_lotteries[i][@"name"]])?YES:NO;
        button.tag = 300 + i;
        [self addSubview:button];
        
        NSUInteger row = i / 3;
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(width, height));
            make.left.mas_equalTo(self).with.offset(offset_x + (width + offset_x) * (i % 3));
            make.top.mas_equalTo(self).with.offset(offset_x + (height + offset_x) * row);
        }];
        
        
    }
}

- (void)actionForBtn:(CYSelectBtn *)sender{
    
    sender.selected = YES;
    [self ergodicSubView:^(CYView *view) {
        CYSelectBtn *button = (CYSelectBtn *)view;
        if (button.tag != sender.tag && button.tag >= 300)
            button.selected = NO;
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        _myblock(sender.tag - 300);
    }];
    
}

- (void)changeCurrentIndex:(NSUInteger)index{
    
    [self ergodicSubView:^(CYView *view) {
        CYSelectBtn *button = (CYSelectBtn *)view;
        if (button.tag == 300 + index)
            [self performSelector:@selector(actionForBtn:) withObject:button];
    }];
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
